package ch.bfh.ti.pixelator.send;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SendPanel extends SurfaceView implements SurfaceHolder.Callback {
	private PaintThread thread;
	private Paint paint = new Paint();
	private int[] data = null;
	private Rect rectData;
	private Rect rectDisplay;
	private Bitmap bitmap;

	private void init() {
		getHolder().addCallback(this);

		setKeepScreenOn(true);
		
		setFocusable(true);

		paint.setAntiAlias(true);
		paint.setColor(Color.BLACK);
	}

	public void setData(int[] data, int width, int height) {
		this.data = data;
		rectData = new Rect(0, 0, width, height);
		bitmap = Bitmap.createBitmap(rectData.width(), rectData.height(),
				Bitmap.Config.ARGB_8888);
		bitmap.setPixels(data, 0, rectData.width(), 0, 0, rectData.width(),
				rectData.height());
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		rectDisplay = new Rect(10, 10, w-10, w-10);
	}
	
	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);
	}

	public SendPanel(Context context) {
		super(context);
		init();
	}

	public SendPanel(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public SendPanel(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		thread = new PaintThread(getHolder(), this);
		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		thread.setRunning(false);
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}
	}

	@Override
	public void onDraw(Canvas canvas) {
		if (canvas == null || bitmap == null || data == null)
			return;
		
		canvas.drawColor(Color.WHITE);
		canvas.drawBitmap(bitmap, rectData, rectDisplay, paint);
	}

	public class PaintThread extends Thread {
		private SurfaceHolder surfaceHolder;
		private SendPanel panel;
		private boolean run = false;

		public PaintThread(SurfaceHolder surfaceHolder, SendPanel panel) {
			this.surfaceHolder = surfaceHolder;
			this.panel = panel;
		}

		public void setRunning(boolean run) {
			this.run = run;
		}

		@SuppressLint("WrongCall")
		@Override
		public void run() {
			Canvas c;
			while (run) {
				try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				c = null;
				try {
					c = surfaceHolder.lockCanvas(null);
					synchronized (surfaceHolder) {
						panel.onDraw(c);
					}
				} finally {
					if (c != null) {
						surfaceHolder.unlockCanvasAndPost(c);
					}
				}
			}
		}
	}

}
